<?php
class Shared
{
    public static function make_message($params)
    {
        $view_name = 'message';
        $params = (object) $params;

        if (!View::exists('name: ' . $view_name)) {
            throw new ErrorException("Không tìm thấy View [{$view_name}]");
        } elseif (!isset($params->content)) {
            throw new Exception('Not exist Message content');
        }
        $view = View::of($view_name);

        $view->title = isset($params->title) ? $params->title : 'Thông báo';
        $view->content = $params->content;
        $view->redirect = isset($params->redirect_url);
        if ($view->redirect) {
            $view->redirect_url = $params->redirect_url;
            $view->redirect_timeout =
                (isset($params->redirect_timeout) && is_numeric($params->redirect_timeout)) ?
                    $params->redirect_timeout : 5;

            $view->redirect_msg = isset($params->redirect_msg) ? $params->redirect_msg : 'Đang chuyển đến trang yêu cầu';
        }
        $view->confirm = isset($params->confirm_submit_url);
        if ($view->confirm) {
            $view->confirm_submit_label = isset($params->confirm_submit_label) ? $params->confirm_submit_label : 'Đồng ý';
            $view->confirm_cancel_label = isset($params->confirm_cancel_label) ? $params->confirm_cancel_label : 'Hủy bỏ';
            $view->confirm_cancel_url = isset($params->confirm_cancel_url) ? $params->confirm_cancel_url : '/';
            $view->confirm_submit_url = $params->confirm_submit_url;
        }


        $view->describe = isset($params->describe) ? $params->describe : '';
        $view->type = isset($params->type) ? $params->type : 'success';

        return $view;
    }
}